package model;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import factory.HibernateUtil;


public class EditalDao {
	public static List<Edital> buscarTodos() {
        List<Edital> editais = new ArrayList<Edital>();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            editais = session.createQuery("from Edital").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return editais;
    }
	
	public static List<Edital> buscarTodosAtivos() {
        List<Edital> editais = new ArrayList<Edital>();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            editais = session.createQuery("from Edital where status = true").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return editais;
    }
	
	public static List<Edital> buscarTodosInativos() {
        List<Edital> editais = new ArrayList<Edital>();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            editais = session.createQuery("from Edital where status = false").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return editais;
    }
 
 public static void salvar(Edital edital){
	 Transaction trns = null;
	 Session session = HibernateUtil.getSession();
	 try {
		 trns = session.beginTransaction();
		 session.save(edital);
		 session.getTransaction().commit();
	 } catch (RuntimeException e) {
		 if (trns != null) {
			 trns.rollback();
		 }
		 e.printStackTrace();
	 } finally {
		 session.flush();
		 session.close();
	 }
 }
 
 public static Edital buscarPorId(String id) {
        Edital edital = new Edital();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        Query query = session.createQuery("from Edital where idEdital = "+ id);
        try {
            trns = session.beginTransaction();
            edital = (Edital) query.uniqueResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return edital;
    }
 
 public static Edital buscarPorContrato(String contrato) {
     Edital edital = new Edital();
     Transaction trns = null;
     Session session = HibernateUtil.getSession();
     Query query = session.createQuery("from Edital where contrato = '"+ contrato + "'");
     try {
         trns = session.beginTransaction();
         edital = (Edital) query.uniqueResult();
     } catch (RuntimeException e) {
         e.printStackTrace();
     } finally {
         session.flush();
         session.close();
     }
     return edital;
 }
 
 public static void alterarEdital(Edital edital) {
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            session.update(edital);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
 }
 
 public static void excluirEdital(Edital edital) {
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            session.delete(edital);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
}
